package cn.itcast.config;


import cn.itcast.exception.MyExceptionHandledAdvice;
import cn.itcast.pojo.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

//@Aspect
@Component
public class ExceptionAdvice {
    @Autowired
    private MyExceptionHandledAdvice myExceptionHandledAdvice;

    @Around("execution( * cn.itcast.controller.DeptController.*(..))")
    public Object round(ProceedingJoinPoint pjp) {
        try {
            return pjp.proceed();
        } catch (Throwable e) {
            if (e instanceof IOException) {
                return myExceptionHandledAdvice.handleIoException((IOException) e);
            } else if (e instanceof IndexOutOfBoundsException) {
                return myExceptionHandledAdvice.handleIndexOutOfBoundsException((IndexOutOfBoundsException) e);
            } else if (e instanceof NullPointerException) {
                return myExceptionHandledAdvice.handleNullPointerException((NullPointerException) e);
            }
        }
        return null;
    }

}
